package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.ModelDeploymentContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.ModelDeploymentPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ConstanceVariable;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReModelCustomConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Processor - 保存流程级别/节点级别的抄送人配置
 *
 * @author lilh
 * @date 2021/6/7 10:26
 */
@Component
public class SaveCopierConfigProcessor implements ModelDeploymentPostProcessor, CustomPropertiesProcessor {

    @Autowired
    private FlowReModelCustomConfigRepository modelCustomConfigRepository;

    /**
     * 保存模型级别的抄送人配置
     *
     * @param context 部署上下文
     */
    @Override
    public void process(ModelDeploymentContext context) {
        saveCopierConfig(context.getDeployId(), context.getModelJson());
    }

    private void saveCopierConfig(String deployId, JsonNode modelJson) {
        ObjectNode properties = (ObjectNode) modelJson.get("properties");
        JsonNode copierConfigNode = properties.get(ConstanceVariable.CC);
        if (isInvalidConfig(copierConfigNode)) {
            return;
        }

        ModelCustomConfig modelCustomConfig = new ModelCustomConfig();
        modelCustomConfig.setDeploymentId(deployId);
        modelCustomConfig.setType(ConstanceVariable.CC);
        modelCustomConfig.setConfig(copierConfigNode.toString());
        //插入自定义配置
        this.doSaveConfig(modelCustomConfig);
    }

    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            return nonNull(properties.get(ConstanceVariable.CC));
        }
        return false;
    }

    /**
     * 保存节点级别的抄送人配置
     *
     * @param context 处理上下文
     */
    @Override
    public void process(FlowElementProcessContext context) {
        JsonNode copierConfigNode = context.getElementNode().get("properties").get(ConstanceVariable.CC);
        if (isInvalidConfig(copierConfigNode)) {
            return;
        }
        ModelCustomConfig modelCustomConfig = new ModelCustomConfig();
        modelCustomConfig.setDeploymentId(context.getDeployId());
        modelCustomConfig.setType(ConstanceVariable.CC);
        modelCustomConfig.setConfig(copierConfigNode.toString());
        //设置节点编码
        modelCustomConfig.setNodeCode(context.getNodeCode());
        //插入自定义配置
        this.doSaveConfig(modelCustomConfig);
    }

    private boolean isInvalidConfig(JsonNode copierConfigNode) {
        if (Objects.isNull(copierConfigNode) || copierConfigNode.isNull()) {
            return true;
        }
        if (!(copierConfigNode instanceof ArrayNode)) {
            return true;
        }
        return false;
    }

    private void doSaveConfig(ModelCustomConfig modelCustomConfig) {
        modelCustomConfigRepository.addModelCustomConfig(modelCustomConfig);
    }
}
